import { ref, onMounted, onUnmounted } from 'vue'

export function useScroll() {
  const scrollY = ref(0)
  const isScrolling = ref(false)
  const scrollDirection = ref<'up' | 'down'>('down')
  let lastScrollY = 0
  let scrollTimer: number | null = null

  const handleScroll = () => {
    const currentScrollY = window.scrollY
    scrollY.value = currentScrollY
    scrollDirection.value = currentScrollY > lastScrollY ? 'down' : 'up'
    lastScrollY = currentScrollY

    // 检测是否正在滚动
    isScrolling.value = true

    if (scrollTimer) {
      clearTimeout(scrollTimer)
    }

    scrollTimer = window.setTimeout(() => {
      isScrolling.value = false
    }, 150)
  }

  const scrollToElement = (elementId: string, offset = 0) => {
    const element = document.getElementById(elementId)
    if (element) {
      const elementPosition = element.offsetTop - offset
      window.scrollTo({
        top: elementPosition,
        behavior: 'smooth'
      })
    }
  }

  const scrollToTop = () => {
    window.scrollTo({
      top: 0,
      behavior: 'smooth'
    })
  }

  const getElementOffset = (elementId: string) => {
    const element = document.getElementById(elementId)
    return element ? element.offsetTop : 0
  }

  const isElementInViewport = (elementId: string, threshold = 0.1) => {
    const element = document.getElementById(elementId)
    if (!element) return false

    const rect = element.getBoundingClientRect()
    const windowHeight = window.innerHeight || document.documentElement.clientHeight

    return (
      rect.top <= windowHeight * (1 - threshold) &&
      rect.bottom >= windowHeight * threshold
    )
  }

  const getCurrentSection = (sectionIds: string[]) => {
    for (const sectionId of sectionIds) {
      if (isElementInViewport(sectionId, 0.5)) {
        return sectionId
      }
    }
    return sectionIds[0]
  }

  onMounted(() => {
    window.addEventListener('scroll', handleScroll)
    // 初始化滚动位置
    handleScroll()
  })

  onUnmounted(() => {
    window.removeEventListener('scroll', handleScroll)
    if (scrollTimer) {
      clearTimeout(scrollTimer)
    }
  })

  return {
    scrollY,
    isScrolling,
    scrollDirection,
    scrollToElement,
    scrollToTop,
    getElementOffset,
    isElementInViewport,
    getCurrentSection
  }
}

// 节流函数
export function throttle(func: Function, delay: number) {
  let timeoutId: number | null = null
  let lastExecTime = 0

  return function (...args: any[]) {
    const currentTime = Date.now()

    if (currentTime - lastExecTime > delay) {
      func.apply(this, args)
      lastExecTime = currentTime
    } else {
      if (timeoutId) {
        clearTimeout(timeoutId)
      }
      timeoutId = window.setTimeout(() => {
        func.apply(this, args)
        lastExecTime = Date.now()
      }, delay - (currentTime - lastExecTime))
    }
  }
}

// 防抖函数
export function debounce(func: Function, delay: number) {
  let timeoutId: number | null = null

  return function (...args: any[]) {
    if (timeoutId) {
      clearTimeout(timeoutId)
    }
    timeoutId = window.setTimeout(() => {
      func.apply(this, args)
    }, delay)
  }
}